package com.fourinone.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.regex.Pattern;

public class ObjectVal extends LinkedHashMap implements BaseSerializable {
    public void setString(String key, String value) {
        super.put(key, value);
    }
    public String getString (Object key) {
        return (String) super.get(key);
    }
    String getSeparator(Object key) {
        return getString(key).replaceAll("\\\\", "/");
    }
    public ObjectVal getWidely (String widelyKey) {
        ObjectVal obj = new ObjectVal();
        for (Iterator iter = this.keySet().iterator(); iter.hasNext();) {
              String curKey = (String)iter.next();
              if (Pattern.matches(widelyKey, curKey)) {
                  obj.put(curKey, this.getObj(curKey));
              }
        }
        return obj;
    }
    public Object getObj (String keyStr) {
        return super.get(keyStr);
    }
    public ArrayList getObjectNames() {
        ArrayList al = new ArrayList();
        for (Iterator iter = this.keySet().iterator(); iter.hasNext();) {
            al.add(iter.next());
        }
        return al;
    }
    public ArrayList getObjectValues() {
        ArrayList al = new ArrayList();
        for (Iterator iter = this.values().iterator(); iter.hasNext();) {
            al.add(iter.next());
        }
        return al;
    }
    public static void main (String[] args) {
        ObjectVal ov = new ObjectVal();
        ov.setString("1", "1");
        ov.setString("1.1","11");
        ov.setString("1.1.1","1111");
        ov.setString("1.1.1.1","1111");
        ov.setString("1.2","12");
        ov.setString("1.2.1","121");
        ov.setString("2","2");
        ov.setString("22","22");
        ov.setString("domain","1");
        ov.setString("domain._me_ta.version","1");
        ov.setString("domain.node1","1");
        ov.setString("domain.node1._me_ta.version","1");
        ov.setString("domain.node2","1");
        ov.setString("domain.node2._me_ta.version","1");
        ov.setString("domain.node2.a","1");
        ov.setString("domain.node2.a._me_ta.version","1");
        System.out.println(ov.getWidely("1.[^.]*.1"));
        System.out.println(ov.getWidely("1.1.*"));
        System.out.println(ov.getWidely("2\\w"));
        System.out.println(ov.getWidely("domain..*._me_ta.version"));
        System.out.println(ov.getWidely("domain.[^_me_ta]*"));
        String a = "domain.node1._me_ta.version";
        System.out.println(a.substring(0,a.indexOf("._me_ta.")));
    }
}
